package com.ruoyi.OnePipe;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SmsUtils {

    private static final String apiKey = "oAhJFOhciNOTz2qRSFRzMdSxjKBBGM2E";
    private static final String apiSecret = "FXhdHWXhFt6YFPqbLAlTE5YyEPyiN1tSjraJ";
    private static final String signId = "SS_gbLMSjB701";
    private static final String templateId = "ST_Q0L9Q0STPB";


    public static void main(String[] args) throws Exception{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("to","+23408050995364");
        jsonObject.put("sign_id",signId);
        jsonObject.put("template_id",templateId);

        JSONObject params = new JSONObject();
        params.put("code","123123");
        jsonObject.put("template_params",params);

        String sha256StrJava = getSHA256StrJava(jsonObject.toString());

        String date = DateUtils.dateTime(new Date(),"yyyy-MM-dd'T'HH:mm:ss.SSSZ");

        String content = date+"\n"+sha256StrJava;

        System.out.println("content:"+content);


        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKey = new SecretKeySpec(apiSecret.getBytes("utf-8"), "HmacSHA256");
        sha256_HMAC.init(secretKey);
        byte[] hash = sha256_HMAC.doFinal(content.getBytes("utf-8"));
        String signature=byte2Hex(hash);

        String authorization = apiKey+":"+date+":"+signature;

        System.out.println("准备发送");
        Map<String,String> map = new HashMap<>();
        map.put("Authorization",authorization);

        System.out.println(map.get("Authorization"));
        System.out.println(jsonObject);
    }




    /**
     * 利用java原生的摘要实现SHA256加密
     * @param str 加密后的报文
     * @return
     */
    public static String getSHA256StrJava(String str){
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }
    /**
     * 将byte转为16进制
     * @param bytes
     * @return
     */
    private static String byte2Hex(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i=0;i<bytes.length;i++){
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length()==1){
                //1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }


}
